﻿using System;
using System.Data;

namespace PublicDomain.Data.Csv
{
    public class DataRowToDataRecordAdapter : IDataRecord
    {
        public DataRowToDataRecordAdapter()
        {
        }

        public DataRowToDataRecordAdapter(DataRow dataRow)
        {
            this.DataRow = dataRow;
        }

        public DataRow DataRow { get; set; }

        public int FieldCount
        {
            get { return this.DataRow.Table.Columns.Count; }
        }

        public object this[string name]
        {
            get
            {
                var result = this.DataRow[name];
                return result;
            }
        }

        public object this[int i]
        {
            get
            {
                var result = this.DataRow[i];
                return result;
            }
        }

        public bool GetBoolean(int i)
        {
            var result = this.DataRow.Field<bool>(i);
            return result;
        }

        public byte GetByte(int i)
        {
            var result = this.DataRow.Field<byte>(i);
            return result;
        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException();
        }

        public char GetChar(int i)
        {
            var result = this.DataRow.Field<char>(i);
            return result;
        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            throw new NotImplementedException();
        }

        public IDataReader GetData(int i)
        {
            throw new NotImplementedException();
        }

        public string GetDataTypeName(int i)
        {
            var result = this.DataRow.Table.Columns[i].DataType.Name;
            return result;
        }

        public DateTime GetDateTime(int i)
        {
            var result = this.DataRow.Field<DateTime>(i);
            return result;
        }

        public decimal GetDecimal(int i)
        {
            var result = this.DataRow.Field<decimal>(i);
            return result;
        }

        public double GetDouble(int i)
        {
            var result = this.DataRow.Field<double>(i);
            return result;
        }

        public Type GetFieldType(int i)
        {
            var result = this.DataRow.Table.Columns[i].DataType;
            return result;
        }

        public float GetFloat(int i)
        {
            var result = this.DataRow.Field<float>(i);
            return result;
        }

        public Guid GetGuid(int i)
        {
            var result = this.DataRow.Field<Guid>(i);
            return result;
        }

        public short GetInt16(int i)
        {
            var result = this.DataRow.Field<short>(i);
            return result;
        }

        public int GetInt32(int i)
        {
            var result = this.DataRow.Field<int>(i);
            return result;
        }

        public long GetInt64(int i)
        {
            var result = this.DataRow.Field<long>(i);
            return result;
        }

        public string GetName(int i)
        {
            var result = this.DataRow.Table.Columns[i].ColumnName;
            return result;
        }

        public int GetOrdinal(string name)
        {
            var result = this.DataRow.Table.Columns[name].Ordinal;
            return result;
        }

        public string GetString(int i)
        {
            var result = this.DataRow.Field<string>(i);
            return result;
        }

        public object GetValue(int i)
        {
            var result = this.DataRow[i];
            return result;
        }

        public int GetValues(object[] values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            int result = 0;
            for (int i = 0; i < values.Length && i < this.FieldCount; i++)
            {
                values[i] = this.GetValue(i);
                result++;
            }

            return result;
        }

        public bool IsDBNull(int i)
        {
            var result = this.DataRow.IsNull(i);
            return result;
        }
    }
}
